/*
 * Copyright (c) United Binary LLC.  All rights reserved.
 * 
 * This code is licensed under the MIT License
 * 
 * SEE: http://harnessit.codeplex.com/license
 * 
 */
using System;
using System.Diagnostics;
using System.Collections;


namespace UnitedBinary.Core.Windows.Forms.Stock
{
	/// <include file='Stock.xml' path='/Docs/TipOfTheDay/Class/*'/>
	public class TipOfTheDay
	{
		private ArrayList tips = new ArrayList();
		private int[] randomOrdering = new int [0];
		private int position = 0;

		/// <include file='Stock.xml' path='/Docs/TipOfTheDay/ctor/*'/>
		public TipOfTheDay()
		{
		}

		/// <include file='Stock.xml' path='/Docs/TipOfTheDay/Add/*'/>
		public void Add(string tip)
		{
			tips.Add(tip);
			Randomize();
		}

		/// <include file='Stock.xml' path='/Docs/TipOfTheDay/Clear/*'/>
		public void Clear()
		{
			tips.Clear();
			Randomize();
		}

		/// <include file='Stock.xml' path='/Docs/TipOfTheDay/Count/*'/>
		public int Count
		{
			get
			{
				return tips.Count;
			}
		}

		/// <include file='Stock.xml' path='/Docs/TipOfTheDay/GetNextTip/*'/>
		public string GetNextTip()
		{
			if (tips.Count == 0)
			{
				return "No tips available.";
			}

			int tipIndex = randomOrdering[position];
			string tip = (string)tips[tipIndex];
			position = (position+1)%(tips.Count);

			return tip;
		}

		private void Randomize()
		{
			if (tips.Count == 0)
			{
				return;
			}

			randomOrdering = new int [tips.Count];

			System.Random rnd = new System.Random();
			bool done = false;
			for (int i=0; i < tips.Count; i++)
			{
				done = false;
				while (!done)
				{
					randomOrdering[i] = rnd.Next(tips.Count);
					done = true;
					for (int k=0; k < i; k++)
					{
						if (randomOrdering[k] == randomOrdering[i])
						{
							done = false;
						}
					}
				}
			}
		}
	}
}
